/**
 * 
 */
package org.hotpotmaterial.codegenn2.controller.v1.admin;

import java.util.List;

import org.hotpotmaterial.codegenn2.common.ResultCodeConstants;
import org.hotpotmaterial.codegenn2.dto.ResultBaseDTO;
import org.hotpotmaterial.codegenn2.dto.ResultPageDTO;
import org.hotpotmaterial.codegenn2.dto.entity.PageDTO;
import org.hotpotmaterial.codegenn2.dto.entity.v1.ProjectDTO;
import org.hotpotmaterial.codegenn2.dto.request.PageRequestDTO;
import org.hotpotmaterial.codegenn2.entity.Project;
import org.hotpotmaterial.codegenn2.service.IProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;

import io.swagger.annotations.Api;

/**
 * @author Wenxing
 *
 */
@Controller("adminProjectController")
@RequestMapping("/hmteam/codegen/admin/v1/projects")
@Api(value = "Admin项目Controller", tags={"Admin项目管理"})
public class ProjectController {
	
	@Autowired
	private IProjectService projectService;
	
	/**
	 * 所有项目分页
	 * @param searchDTO
	 * @return
	 */
	@PostMapping("/pages")
	public ResponseEntity<ResultBaseDTO> postPages(@RequestBody PageRequestDTO searchDTO) {
		// 业务
		PageDTO<Project> serviceResult = projectService.findProjects(searchDTO);
		// 转换
		List<ProjectDTO> dataList = Lists.newArrayList();
		serviceResult.getData().forEach((item) -> {
			ProjectDTO data = new ProjectDTO();
			BeanUtils.copyProperties(item, data);
			dataList.add(data);
		});
		return new ResponseEntity<>(
				ResultPageDTO.<ProjectDTO>builder().pageNum(serviceResult.getPageNum())
						.pageSize(serviceResult.getPageSize()).total(serviceResult.getTotal()).data(dataList)
						.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build(),
				HttpStatus.OK);
	}

}
